/*
========================================================================
 Name        : FRamI_UISettingListView.cpp
 Author      : Lenka Litvova
 Copyright   : Copyright belongs to Lenka Litvova
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>
#include <aknnotewrappers.h>
#include <FRamI_UI.rsg>
// ]]] end generated region [Generated System Includes]

#include <stringloader.h>
#include <barsread.h>
#include "FRamI_UIAppUi.h"

// [[[ begin generated region: do not modify [Generated User Includes]

#include "FRamI_UI.hrh"
#include "FRamI_UISettingListView.h"
#include "FRamI_UIMainContainer.hrh"
#include "FRamI_UISettingList.hrh"
#include "FRamI_UIUsersList.hrh"
#include "FRamI_UISettingList.h"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CFRamI_UISettingListView::CFRamI_UISettingListView(CConfiguration *aConfiguration):
iConfiguration(aConfiguration)
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CFRamI_UISettingListView::~CFRamI_UISettingListView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	if(iConfiguration)
		delete iConfiguration;
	iConfiguration = NULL;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CFRamI_UISettingListView
 */
CFRamI_UISettingListView* CFRamI_UISettingListView::NewL(CConfiguration *aConfiguration)
	{
	CFRamI_UISettingListView* self = CFRamI_UISettingListView::NewLC(aConfiguration);
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CFRamI_UISettingListView
 */
CFRamI_UISettingListView* CFRamI_UISettingListView::NewLC(CConfiguration *aConfiguration)
	{
	CFRamI_UISettingListView* self = new ( ELeave ) CFRamI_UISettingListView(aConfiguration);
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CFRamI_UISettingListView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_FRAM_I_UISETTING_LIST_FRAM_I_UISETTING_LIST_VIEW );
				
	// ]]] end generated region [Generated Code]
	
	// add your own initialization code here
	
	}

/**
 * @return The UID for this view
 */
TUid CFRamI_UISettingListView::Id() const
	{
	return TUid::Uid( EFRamI_UISettingListViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CFRamI_UISettingListView::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case EFRamI_UISettingListViewMenuItem1Command:
			commandHandled = HandleChangeSelectedSettingItemL( aCommand );
			break;
		case EFRamI_UISettingListViewSaveMenuItemCommand:
			commandHandled = HandleSaveMenuItemSelectedL( aCommand );
			break;
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
	
		}
	// ]]] end generated region [Generated Code]
	switch(aCommand)
	{
	case EAknSoftkeyBack:
		{
			((CFRamI_UIAppUi*)AppUi())->ShowMainView();
			break;
		}
	case EFRamI_UISettingListViewSearchForFileCommand:
		{
			((CFRamI_UIAppUi*)AppUi())->SearchForSoundFile();
			break;
		}
	}
	
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CFRamI_UISettingListView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
				
				
	
	if ( iFRamI_UISettingList == NULL )
		{
		iSettings = TFRamI_UISettingListSettings::NewL();
		iFRamI_UISettingList = new ( ELeave ) CFRamI_UISettingList( *iSettings, this, iConfiguration );
		iFRamI_UISettingList->SetMopParent( this );
		iFRamI_UISettingList->ConstructFromResourceL( R_FRAM_I_UISETTING_LIST_FRAM_I_UISETTING_LIST );
		iFRamI_UISettingList->ActivateL();
		iFRamI_UISettingList->LoadSettingValuesL();
		iFRamI_UISettingList->LoadSettingsL();
		AppUi()->AddToStackL( *this, iFRamI_UISettingList );
		} 
	// ]]] end generated region [Generated Contents]
	
	}

/**
 */
void CFRamI_UISettingListView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	if ( iFRamI_UISettingList != NULL )
		{
		AppUi()->RemoveFromStack( iFRamI_UISettingList );
		delete iFRamI_UISettingList;
		iFRamI_UISettingList = NULL;
		delete iSettings;
		iSettings = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CFRamI_UISettingListView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}

// [[[ begin generated function: do not modify
void CFRamI_UISettingListView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_FRAM_I_UISETTING_LIST_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

// ]]] end generated function

// [[[ begin generated function: do not modify
void CFRamI_UISettingListView::CleanupStatusPane()
	{
	}

// ]]] end generated function

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CFRamI_UISettingListView::HandleChangeSelectedSettingItemL( TInt aCommand )
	{
	iFRamI_UISettingList->ChangeSelectedItemL();
	return ETrue;
	}
				

// [[[ begin generated function: do not modify
/**
 * Show the popup note for errorNote
 * @param aOverrideText optional override text
 */
void CFRamI_UISettingListView::RunErrorNoteL( const TDesC* aOverrideText )
	{
	CAknErrorNote* note = new ( ELeave ) CAknErrorNote();
	if ( aOverrideText == NULL )
		{
		HBufC* noteText = StringLoader::LoadLC( R_FRAM_I_UISETTING_LIST_ERROR_NOTE );
		note->ExecuteLD( *noteText );
		CleanupStack::PopAndDestroy( noteText );
		}
	else
		{
		note->ExecuteLD( *aOverrideText );
		}
	}
// ]]] end generated function

//moje
void CFRamI_UISettingListView::SetPath(const TDesC &aPath)
	{
		iConfiguration->SetSoundFile(aPath);
	}

void CFRamI_UISettingListView::ResetConfiguration()
	{
		if(iConfiguration)
			delete iConfiguration;
		iConfiguration = NULL;
		iConfiguration = CConfiguration::NewL();
		iConfiguration->LoadSettingFromFileL();
	}

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CFRamI_UISettingListView::HandleSaveMenuItemSelectedL( TInt aCommand )
	{
		TInt check = iConfiguration->IsOk();
		HBufC* text;
		switch(iConfiguration->IsOk())
		{
		case 0:
			iConfiguration->SaveSettingToFileL();
			((CFRamI_UIAppUi*)AppUi())->ShowMainView();
			break;
		case 1:
			text = StringLoader::LoadLC( R_FRAM_I_UISETTING_LIST_MISSING_SOUND );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;
		case 2:
			text = StringLoader::LoadLC(R_FRAM_I_UISETTING_LIST_BAD_SOUND );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;
		case 3:
			text = StringLoader::LoadLC( R_FRAM_I_UISETTING_LIST_MISSING_PHONE );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;
		case 4:
			text = StringLoader::LoadLC( R_FRAM_I_UISETTING_LIST_MISSING_MAIL );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;
		case 5:
			text = StringLoader::LoadLC(R_FRAM_I_UISETTING_LIST_BAD_MAIL );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;		
		case 6:
			text = StringLoader::LoadLC( R_FRAM_I_UISETTING_LIST_MISSING_PIN );
			RunErrorNoteL(text);
			CleanupStack::PopAndDestroy( text );		
			break;
		}
	return ETrue;
	}
				
